home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH9 / SRC / ROTATE.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-11-16  |  12.6 KB  |  420 lines

  1. VERSION 4.00
  2. Begin VB.Form RotateForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Rotation Around a Line"
  6.    ClientHeight    =   5415
  7.    ClientLeft      =   300
  8.    ClientTop       =   1020
  9.    ClientWidth     =   9015
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6105
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5415
  25.    ScaleWidth      =   9015
  26.    Top             =   390
  27.    Width           =   9135
  28.    Begin VB.PictureBox Pict 
  29.       AutoRedraw      =   -1  'True
  30.       Height          =   2175
  31.       Index           =   7
  32.       Left            =   6840
  33.       ScaleHeight     =   -8
  34.       ScaleLeft       =   -4
  35.       ScaleMode       =   0  'User
  36.       ScaleTop        =   4
  37.       ScaleWidth      =   8
  38.       TabIndex        =   7
  39.       Top             =   2760
  40.       Width           =   2175
  41.    End
  42.    Begin VB.PictureBox Pict 
  43.       AutoRedraw      =   -1  'True
  44.       Height          =   2175
  45.       Index           =   6
  46.       Left            =   4560
  47.       ScaleHeight     =   -8
  48.       ScaleLeft       =   -4
  49.       ScaleMode       =   0  'User
  50.       ScaleTop        =   4
  51.       ScaleWidth      =   8
  52.       TabIndex        =   6
  53.       Top             =   2760
  54.       Width           =   2175
  55.    End
  56.    Begin VB.PictureBox Pict 
  57.       AutoRedraw      =   -1  'True
  58.       Height          =   2175
  59.       Index           =   5
  60.       Left            =   2280
  61.       ScaleHeight     =   -8
  62.       ScaleLeft       =   -4
  63.       ScaleMode       =   0  'User
  64.       ScaleTop        =   4
  65.       ScaleWidth      =   8
  66.       TabIndex        =   5
  67.       Top             =   2760
  68.       Width           =   2175
  69.    End
  70.    Begin VB.PictureBox Pict 
  71.       AutoRedraw      =   -1  'True
  72.       Height          =   2175
  73.       Index           =   4
  74.       Left            =   0
  75.       ScaleHeight     =   -8
  76.       ScaleLeft       =   -4
  77.       ScaleMode       =   0  'User
  78.       ScaleTop        =   4
  79.       ScaleWidth      =   8
  80.       TabIndex        =   4
  81.       Top             =   2760
  82.       Width           =   2175
  83.    End
  84.    Begin VB.PictureBox Pict 
  85.       AutoRedraw      =   -1  'True
  86.       Height          =   2175
  87.       Index           =   3
  88.       Left            =   6840
  89.       ScaleHeight     =   -8
  90.       ScaleLeft       =   -4
  91.       ScaleMode       =   0  'User
  92.       ScaleTop        =   4
  93.       ScaleWidth      =   8
  94.       TabIndex        =   3
  95.       Top             =   0
  96.       Width           =   2175
  97.    End
  98.    Begin VB.PictureBox Pict 
  99.       AutoRedraw      =   -1  'True
  100.       Height          =   2175
  101.       Index           =   2
  102.       Left            =   4560
  103.       ScaleHeight     =   -8
  104.       ScaleLeft       =   -4
  105.       ScaleMode       =   0  'User
  106.       ScaleTop        =   4
  107.       ScaleWidth      =   8
  108.       TabIndex        =   2
  109.       Top             =   0
  110.       Width           =   2175
  111.    End
  112.    Begin VB.PictureBox Pict 
  113.       AutoRedraw      =   -1  'True
  114.       Height          =   2175
  115.       Index           =   1
  116.       Left            =   2280
  117.       ScaleHeight     =   -8
  118.       ScaleLeft       =   -4
  119.       ScaleMode       =   0  'User
  120.       ScaleTop        =   4
  121.       ScaleWidth      =   8
  122.       TabIndex        =   1
  123.       Top             =   0
  124.       Width           =   2175
  125.    End
  126.    Begin VB.PictureBox Pict 
  127.       AutoRedraw      =   -1  'True
  128.       Height          =   2175
  129.       Index           =   0
  130.       Left            =   0
  131.       ScaleHeight     =   -8
  132.       ScaleLeft       =   -4
  133.       ScaleMode       =   0  'User
  134.       ScaleTop        =   4
  135.       ScaleWidth      =   8
  136.       TabIndex        =   0
  137.       Top             =   0
  138.       Width           =   2175
  139.    End
  140.    Begin VB.Label Label1 
  141.       Alignment       =   2  'Center
  142.       Caption         =   "Reverse translation"
  143.       Height          =   255
  144.       Index           =   7
  145.       Left            =   6840
  146.       TabIndex        =   15
  147.       Top             =   5040
  148.       Width           =   2175
  149.       WordWrap        =   -1  'True
  150.    End
  151.    Begin VB.Label Label1 
  152.       Alignment       =   2  'Center
  153.       Caption         =   "Reverse 1st rotation"
  154.       Height          =   255
  155.       Index           =   6
  156.       Left            =   4560
  157.       TabIndex        =   14
  158.       Top             =   5040
  159.       Width           =   2175
  160.       WordWrap        =   -1  'True
  161.    End
  162.    Begin VB.Label Label1 
  163.       Alignment       =   2  'Center
  164.       Caption         =   "Reverse 2nd rotation"
  165.       Height          =   255
  166.       Index           =   5
  167.       Left            =   2280
  168.       TabIndex        =   13
  169.       Top             =   5040
  170.       Width           =   2175
  171.       WordWrap        =   -1  'True
  172.    End
  173.    Begin VB.Label Label1 
  174.       Alignment       =   2  'Center
  175.       Caption         =   "Rotate around line"
  176.       Height          =   255
  177.       Index           =   4
  178.       Left            =   0
  179.       TabIndex        =   12
  180.       Top             =   5040
  181.       Width           =   2175
  182.       WordWrap        =   -1  'True
  183.    End
  184.    Begin VB.Label Label1 
  185.       Alignment       =   2  'Center
  186.       Caption         =   "Rotate into Y axis"
  187.       Height          =   255
  188.       Index           =   3
  189.       Left            =   6840
  190.       TabIndex        =   11
  191.       Top             =   2280
  192.       Width           =   2175
  193.       WordWrap        =   -1  'True
  194.    End
  195.    Begin VB.Label Label1 
  196.       Alignment       =   2  'Center
  197.       Caption         =   "Rotate into Y-Z plane"
  198.       Height          =   255
  199.       Index           =   2
  200.       Left            =   4560
  201.       TabIndex        =   10
  202.       Top             =   2280
  203.       Width           =   2175
  204.       WordWrap        =   -1  'True
  205.    End
  206.    Begin VB.Label Label1 
  207.       Alignment       =   2  'Center
  208.       Caption         =   "Translate to origin"
  209.       Height          =   255
  210.       Index           =   1
  211.       Left            =   2280
  212.       TabIndex        =   9
  213.       Top             =   2280
  214.       Width           =   2175
  215.       WordWrap        =   -1  'True
  216.    End
  217.    Begin VB.Label Label1 
  218.       Alignment       =   2  'Center
  219.       Caption         =   "Original picture"
  220.       Height          =   255
  221.       Index           =   0
  222.       Left            =   0
  223.       TabIndex        =   8
  224.       Top             =   2280
  225.       Width           =   2175
  226.       WordWrap        =   -1  'True
  227.    End
  228.    Begin VB.Menu mnuFile 
  229.       Caption         =   "&File"
  230.       Begin VB.Menu mnuFileExit 
  231.          Caption         =   "E&xit"
  232.       End
  233.    End
  234. Attribute VB_Name = "RotateForm"
  235. Attribute VB_Creatable = False
  236. Attribute VB_Exposed = False
  237. Option Explicit
  238. ' The line around which to rotate.
  239. Const Px = 0
  240. Const Py = 3
  241. Const Pz = 0
  242. Const Vx = 1
  243. Const Vy = 1
  244. Const Vz = 1
  245. ' Location of viewing eye.
  246. Dim EyeR As Single
  247. Dim EyeTheta As Single
  248. Dim EyePhi As Single
  249. ' Location of focus point.
  250. Const FocusX = 0#
  251. Const FocusY = 0#
  252. Const FocusZ = 0#
  253. Dim Projector(1 To 4, 1 To 4) As Single
  254. ' Matrices used for the rotation.
  255. Dim Trans(1 To 4, 1 To 4) As Single
  256. Dim Rot1(1 To 4, 1 To 4) As Single
  257. Dim Rot2(1 To 4, 1 To 4) As Single
  258. Dim Rot3(1 To 4, 1 To 4) As Single
  259. Dim Rot2_Inv(1 To 4, 1 To 4) As Single
  260. Dim Rot1_Inv(1 To 4, 1 To 4) As Single
  261. Dim Trans_Inv(1 To 4, 1 To 4) As Single
  262. Dim M(0 To 7) As Transformation
  263. ' ***********************************************
  264. ' Create the matrices used when rotating through
  265. ' angle theta around a line passing through
  266. ' (p1, p2, p3) in direction <d1, d2, d3>. Theta
  267. ' is measured counterclockwise as you look down
  268. ' the line opposite the line's direction.
  269. ' ***********************************************
  270. Sub CreateMatrices(p1 As Single, p2 As Single, p3 As Single, d1 As Single, d2 As Single, d3 As Single, theta As Single)
  271. Dim D As Single
  272. Dim L As Single
  273.     ' Translate the line to the origin.
  274.     m3Translate Trans, -p1, -p2, -p3
  275.     m3Translate Trans_Inv, p1, p2, p3
  276.     ' Rotate around Z-axis until the line is in
  277.     ' the Y-Z plane.
  278.     m3Identity Rot1
  279.     D = Sqr(d1 * d1 + d2 * d2)
  280.     Rot1(1, 1) = d2 / D
  281.     Rot1(1, 2) = d1 / D
  282.     Rot1(2, 1) = -Rot1(1, 2)
  283.     Rot1(2, 2) = Rot1(1, 1)
  284.     m3Identity Rot1_Inv
  285.     Rot1_Inv(1, 1) = Rot1(1, 1)
  286.     Rot1_Inv(1, 2) = -Rot1(1, 2)
  287.     Rot1_Inv(2, 1) = -Rot1(2, 1)
  288.     Rot1_Inv(2, 2) = Rot1(2, 2)
  289.     ' Rotate around the X-axis until the line
  290.     ' lies along the Y axis.
  291.     m3Identity Rot2
  292.     L = Sqr(d1 * d1 + d2 * d2 + d3 * d3)
  293.     Rot2(2, 2) = D / L
  294.     Rot2(2, 3) = -d3 / L
  295.     Rot2(3, 2) = -Rot2(2, 3)
  296.     Rot2(3, 3) = Rot2(2, 2)
  297.     m3Identity Rot2_Inv
  298.     Rot2_Inv(2, 2) = Rot2(2, 2)
  299.     Rot2_Inv(2, 3) = -Rot2(2, 3)
  300.     Rot2_Inv(3, 2) = -Rot2(3, 2)
  301.     Rot2_Inv(3, 3) = Rot2(3, 3)
  302.     ' Rotate around the Y axis.
  303.     m3YRotate Rot3, theta
  304.     ' Put the matrices in the M array.
  305.     m3Identity M(0).M
  306.     m3MatCopy M(1).M, Trans
  307.     m3MatCopy M(2).M, Rot1
  308.     m3MatCopy M(3).M, Rot2
  309.     m3MatCopy M(4).M, Rot3
  310.     m3MatCopy M(5).M, Rot2_Inv
  311.     m3MatCopy M(6).M, Rot1_Inv
  312.     m3MatCopy M(7).M, Trans_Inv
  313. End Sub
  314. ' ***********************************************
  315. ' Let the user change the location of the eye.
  316. ' ***********************************************
  317. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  318. Const Dtheta = PI / 20
  319.     Select Case KeyCode
  320.         Case vbKeyLeft
  321.             EyeTheta = EyeTheta - Dtheta
  322.             
  323.         Case vbKeyRight
  324.             EyeTheta = EyeTheta + Dtheta
  325.         
  326.         Case vbKeyUp
  327.             EyePhi = EyePhi - Dtheta
  328.         
  329.         Case vbKeyDown
  330.             EyePhi = EyePhi + Dtheta
  331.         
  332.         Case Else
  333.             Exit Sub
  334.     End Select
  335.     ' Redraw the pictures.
  336.     DrawTheData
  337. End Sub
  338. ' *******************************************************
  339. ' Rotate the points in the cube and draw the cube.
  340. ' *******************************************************
  341. Private Sub DrawData(pic As Object)
  342. Dim i As Integer
  343. Dim x1 As Single
  344. Dim y1 As Single
  345. Dim x2 As Single
  346. Dim y2 As Single
  347. Dim oldwidth As Integer
  348.     ' Compute the projection matrix.
  349.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  350.     ' Transform the points.
  351.     TransformAllData Projector
  352.     ' Draw the points.
  353.     pic.Cls
  354.     oldwidth = pic.DrawWidth
  355.     For i = 1 To NumSegments
  356.         x1 = Segments(i).fr_tr(1)
  357.         y1 = Segments(i).fr_tr(2)
  358.         x2 = Segments(i).to_tr(1)
  359.         y2 = Segments(i).to_tr(2)
  360.         
  361.         ' Draw the plane's normal in bold.
  362.         If i = 4 Then pic.DrawWidth = 3
  363.         pic.Line (x1, y1)-(x2, y2)
  364.         If i = 4 Then pic.DrawWidth = oldwidth
  365.     Next i
  366.     pic.Refresh
  367. End Sub
  368. Private Sub Form_Load()
  369.     ' Initialize the eye position.
  370.     EyeR = 3
  371.     EyeTheta = PI * 0.4
  372.     EyePhi = PI * 0.1
  373.     ' Create the rotation matrices.
  374.     CreateMatrices Px, Py, Pz, Vx, Vy, Vz, PI * 0.1
  375.     ' Create, project, and draw the data.
  376.     DrawTheData
  377. End Sub
  378. ' ***********************************************
  379. ' Draw all the pictures.
  380. ' ***********************************************
  381. Sub DrawTheData()
  382. Dim i As Integer
  383.     ' Start with fresh data.
  384.     CreateData
  385.     For i = 0 To 7
  386.         ' Apply the next transformation.
  387.         TransformData M(i).M, 4, NumSegments
  388.         SetPoints 4, NumSegments
  389.         
  390.         ' Display the data.
  391.         DrawData Pict(i)
  392.     Next i
  393. End Sub
  394. Sub CreateData()
  395.     ' Start with no data.
  396.     NumSegments = 0
  397.     ' Create the axes.
  398.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  399.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  400.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  401.     ' Create the line's direction vector.
  402.     MakeSegment Px, Py, Pz, Px + Vx, Py + Vy, Pz + Vz
  403.     ' Create the object to reflect.
  404.     MakeSegment 1, 1, 1, 1, 1, 3
  405.     MakeSegment 1, 1, 3, 3, 1, 3
  406.     MakeSegment 3, 1, 3, 3, 1, 1
  407.     MakeSegment 3, 1, 1, 1, 1, 1
  408.     MakeSegment 1, 3, 1, 1, 3, 3
  409.     MakeSegment 1, 3, 3, 3, 3, 3
  410.     MakeSegment 3, 3, 3, 3, 3, 1
  411.     MakeSegment 3, 3, 1, 1, 3, 1
  412.     MakeSegment 1, 1, 1, 1, 3, 1
  413.     MakeSegment 1, 1, 3, 1, 3, 3
  414.     MakeSegment 3, 1, 3, 3, 3, 3
  415.     MakeSegment 3, 1, 1, 3, 3, 1
  416. End Sub
  417. Private Sub mnuFileExit_Click()
  418.     Unload Me
  419. End Sub
  420.